package com.example.demo.controller;

import com.example.demo.config.GetBeanUtil;
import com.example.demo.config.RedisService;
import com.example.demo.exceptionhandler.BusinessException;
import com.example.demo.exceptionhandler.EmBusinessError;
import com.example.demo.service.PrintService;
import com.example.demo.threadpool.PoolConfig;
import com.example.demo.threadpool.ThreadSingle;
import com.example.demo.view.TestView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author whn
 */
@RestController
@RequestMapping("/test")
@CrossOrigin
@Api("测试接口API!")
public class TestController {
    @Autowired
    private PrintService printService;

    RedisService redisService = (RedisService) GetBeanUtil.getBean("getRedisService");

    @GetMapping("/get")
    public String get(@RequestParam("flag") String csDbid, @RequestParam("csDbid") String flag) {
        return "csdbid=" + csDbid + "    flag=" + flag;
    }

    @PostMapping("/getPost")
    public String getPost(@RequestBody TestView testView) {
        printService.print(testView.getCsDbid(), testView.getFlag());
        return "完成";
    }

    @GetMapping("/setName")
    public void setName(@RequestParam("name") String name) {
        if (name.equals("")) {
            throw new BusinessException(EmBusinessError.DISHIES_TYPE, "kong");
        }
        redisService.set("name", name);
    }

    @GetMapping("/getName")
    public List<Object> getName() throws InterruptedException {
        List<Object> list = Collections.synchronizedList(new ArrayList<>());
        for (int i = 0; i < 53; i++) {
            list.add(redisService.get("name"));
            Thread.sleep(100);
        }
        return list;
    }

    @GetMapping("/thread")
    @ApiOperation("多线程处理接口")
    public List<Object> thread() throws InterruptedException {
        List<Object> list = Collections.synchronizedList(new ArrayList<>());
        ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) GetBeanUtil.getBean("executor");
        Integer size = 53;
        CountDownLatch countDownLatch = new CountDownLatch(size);
        for (int i = 0; i < size; i++) {
            threadPoolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    list.add(Thread.currentThread().getName() + "取得的结果为：" + redisService.get("name"));
                    countDownLatch.countDown();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Finished all threads");
        return list;
    }
}
